package com.ruoyi.web.controller.system;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.rabbitmq.RabbitMQProducerService;
import com.ruoyi.system.domain.SysNotification;
import com.ruoyi.system.domain.UserNotification;
import com.ruoyi.system.domain.dto.SysNotificationDTO;
import com.ruoyi.system.domain.vo.SysNotificationListVo;
import com.ruoyi.system.service.ISysNotificationService;
import com.ruoyi.system.service.IUserNotificationService;
import com.ruoyi.system.service.impl.UserNotificationServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 通知消息Controller
 *
 * @author xudengyu
 * @date 2025-03-08
 */
@RestController
@RequestMapping("/system/notification")
public class SysNotificationController extends BaseController {
    @Resource
    private ISysNotificationService sysNotificationService;

    @Resource
    private RabbitMQProducerService rabbitMQProducerService;

    @PostMapping("/test-ws-suber")
    public AjaxResult testWSDept(@RequestBody SysNotificationDTO sysNotificationDTO) {
        // 在业务代码中调用
        boolean f=sysNotificationService.pubNotification(sysNotificationDTO);
        return f?AjaxResult.success("消息已发送"):AjaxResult.error("消息发送失败");
    }


    @PostMapping("/test-ws")
    public AjaxResult testWS(@RequestBody SysNotification sysNotification) {
        // 在业务代码中调用
        String userIdStr = getUserId().toString();
        String queueName = "user." + getUserId() + ".queue";
        rabbitMQProducerService.createAndBindQueue(queueName, userIdStr, userIdStr);
        rabbitMQProducerService.sendMessage(userIdStr, JSONUtil.toJsonStr(sysNotification));

        return AjaxResult.success("消息已发送");
    }

    /**
     * 查询通知消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:notification:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotification sysNotification) {
        startPage();
        List<SysNotificationListVo> list = sysNotificationService.selectSysNotificationList(sysNotification);
        return getDataTable(list);
    }

    /**
     * 导出通知消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:notification:export')")
    @Log(title = "通知消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNotification sysNotification) {
        List<SysNotificationListVo> list = sysNotificationService.selectSysNotificationList(sysNotification);
        ExcelUtil<SysNotificationListVo> util = new ExcelUtil<SysNotificationListVo>(SysNotificationListVo.class);
        util.exportExcel(response, list, "通知消息数据");
    }

    /**
     * 获取通知消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notification:query')")
    @GetMapping(value = "/{notificationId}")
    public AjaxResult getInfo(@PathVariable("notificationId") Long notificationId) {
        return success(sysNotificationService.selectSysNotificationByNotificationId(notificationId));
    }

    /**
     * 新增通知消息
     */
    @PreAuthorize("@ss.hasPermi('system:notification:add')")
    @Log(title = "通知消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNotification sysNotification) {
        sysNotification.
                setNotificationPuberId(SecurityUtils.getUserId()).
                setNotificationTime(DateUtils.parseDateToStr("yyyy-MM-dd hh:mm:ss", DateUtils.getNowDate()));
        return toAjax(sysNotificationService.insertSysNotification(sysNotification));
    }

    /**
     * 修改通知消息
     */
    @PreAuthorize("@ss.hasPermi('system:notification:edit')")
    @Log(title = "通知消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNotification sysNotification) {
        return toAjax(sysNotificationService.updateSysNotification(sysNotification));
    }

    /**
     * 删除通知消息
     */
    @PreAuthorize("@ss.hasPermi('system:notification:remove')")
    @Log(title = "通知消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{notificationIds}")
    public AjaxResult remove(@PathVariable Long[] notificationIds) {
        return toAjax(sysNotificationService.deleteSysNotificationByNotificationIds(notificationIds));
    }
}
